package me
import grails.converters.JSON
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource
import groovy.sql.Sql
import groovy.json.*
import groovy.json.JsonSlurper

class DevController {

	def dataSource

    def getoptiongroup() {
        def results = [success:true]
        def sql = new Sql(dataSource)
        def optionCategoryId = params.optionCategoryId
       
        def rows 
        if (optionCategoryId=="root") {
            rows = sql.rows("select * from  option_group where option_category_id is null")
        } else {
            rows = sql.rows("select * from  option_group where option_category_id =?",optionCategoryId as Integer)
        } 
        

        results.groups = rows
        
        println results.groups
        
        render results  as JSON
    }

    def setoptiongroup() {
        
        def results = [success:true]
        def sql = new Sql(dataSource)
        def groupId = params.groupId
        def groupName =  params.groupName
        def optionCategoryId =  params.optionCategoryId
        if (optionCategoryId=="root") {
            def rows = sql.rows("select * from option_group where group_id = ? and option_category_id is null",groupId)
            println rows.size()
            if (rows.size()!=0) {
                sql.execute("update option_group set group_name=? where group_id = ? and option_category_id is null",
                [groupName,groupId])
            } else {
                println 11111
                def optionGroup = new OptionGroup(groupId:groupId,groupName:groupName)
                try {
                    optionGroup.save(failOnError: true,flush: true) 
                } catch (e) {
                    println e
                }
            }
            
        } else {
            def rows = sql.rows("select * from option_group where group_id = ? and option_category_id = ?",[groupId,optionCategoryId as Integer])
            if (rows.size()!=0) {
                sql.execute("update option_group set group_name=? where group_id = ? and option_category_id =?",
                [groupName,groupId,optionCategoryId as Integer])
            } else {
                def optionGroup = new OptionGroup(groupId:groupId,groupName:groupName,optionCategoryId:optionCategoryId as Integer)
                try {
                    optionGroup.save(failOnError: true,flush: true) 
                } catch (e) {
                    println e
                }
            }
        }
        
        
        render results  as JSON

    }

    def setoptionorder() {
        
        def results = [success:true]
        def sql = new Sql(dataSource)
        def id = params.id
        def seq =  params.seq
        
        sql.execute("update base_attrs_info set seq=? where pkid = ?",[seq as Integer,id])
        
        render results  as JSON

    }

    def saveoptionindex() {

        def results = [success:true]
        def sql = new Sql(dataSource)
        def ids = params.ids.split(',')
        def indexs =  params.indexs.split(',')
        println ids
        
        ids.eachWithIndex{it,i->
            println it + "   "+i
            sql.execute("update base_attrs_info set seq=? where pkid = ?",[indexs[i],it])
        }
        
        render results  as JSON

    }

    def savecategoryindex() { 
    	def results = [success:true]
    	def sql = new Sql(dataSource)
    	def ids = params.list('ids')
    	def indexs =  params.list('indexs')
    	println ids
    	
    	ids.eachWithIndex{it,i->
    		println it + "   "+i
    		sql.execute("update base_attrs_info set seq=? where pkid = ?",[indexs[i],it])
		}
    	
		render results  as JSON
    }

    def setoptiondetailmode() {
        def results = [success:true]
        def sql = new Sql(dataSource)
        def id = params.id
        def detailMode = params.detailMode

        if (detailMode==null) {
            sql.execute("update base_attrs_info set detail_flag=null where pkid = ?",id)
        } else {
            sql.execute("update base_attrs_info set detail_flag=concat(ifnull(detail_flag,''),?) where pkid = ?",[detailMode,id])    
        }
        
        
        render results  as JSON
    }

   	
}
